package com.example.dormmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormmanage.dao.IRoleDao;
import com.example.dormmanage.entity.Admin;
import com.example.dormmanage.entity.Role;
import com.example.dormmanage.service.IRoleService;
import com.example.dormmanage.view.ResultView;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleService extends ServiceImpl<IRoleDao, Role> implements IRoleService {
    //查询角色列表
    public ResultView<Page> selectRoleList(Integer page, Integer pageSize, String name) {
        Page<Role> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Role::getRoleName,name);
        pageInfo.setRecords(this.list(lqw));
        return ResultView.success(pageInfo);
    }

    //添加角色
    @Transactional(rollbackFor = Exception.class)
    public ResultView<String> addRole(Role role) {
        this.save(role);
        return ResultView.success("添加角色成功");
    }

    //查询角色详情
    public ResultView<Role> selectAdminData(Integer roleId) {
       return ResultView.success(this.getById(roleId));
    }
}
